using System;
using SqlSugar;
using Infrastructure.Attribute;
using Infrastructure.Extensions;
using ZR.Model;
using ZR.Model.Dto;
using ZR.Model.System;
using ZR.Repository;
using ZR.Service.System.ISystemService;
using System.Linq;
using Org.BouncyCastle.Math.EC.Rfc7748;
using MimeKit.Encodings;
using Aliyun.OSS;

namespace ZR.Service.System
{
    /// <summary>
    /// 生成配置Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(IGenConfigService), ServiceLifetime = LifeTime.Transient)]
    public class GenConfigService : BaseService<GenConfig>, IGenConfigService
    {
        /// <summary>
        /// 查询生成配置列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<GenConfigDto> GetList(GenConfigQueryDto parm)
        {
            var predicate = Expressionable.Create<GenConfig>();

            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.ConfigName), it => it.ConfigName == parm.ConfigName);


            var response = Queryable()
                .Where(predicate.ToExpression())
                .ToPage<GenConfig, GenConfigDto>(parm);

            return response;
        }


        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public GenConfig GetInfo(int Id)
        {
            var response = Queryable()
                .Where(x => x.Id == Id)
                .First();

            return response;
        }

        /// <summary>
        /// 添加生成配置
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public GenConfig AddGenConfig(GenConfig model)
        {

            Update(new GenConfig { ConfigName = model.ConfigName, Enabled = 1 }, it => new { it.Enabled }, f => f.ConfigName == model.ConfigName);


            return Context.Insertable(model).ExecuteReturnEntity();
        }

        /// <summary>
        /// 修改生成配置
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateGenConfig(GenConfig model)
        {
            //var response = Update(w => w.Id == model.Id, it => new GenConfig()
            //{
            //    InitialValueTop = model.InitialValueTop,
            //    UpDownInterval = model.UpDownInterval,
            //    LeftRightInterval = model.LeftRightInterval,
            //    Height = model.Height,
            //    FontName = model.FontName,
            //    FontSize = model.FontSize,
            //    NumberColumns = model.NumberColumns,
            //    ConfigName = model.ConfigName,
            //    Enabled = model.Enabled,
            //    CreateBy = model.CreateBy,
            //    CreateTime = model.CreateTime,
            //    UpdateBy = model.UpdateBy,
            //    UpdateTime = model.UpdateTime,
            //    Remark = model.Remark,
            //});
            //return response;

            //Update(model, i => new { Enabled = 1 }, w => w.FontName == model.FontName);
            Update(new GenConfig { ConfigName = model.ConfigName, Enabled = 1 }, it => new { it.Enabled }, f => f.ConfigName == model.ConfigName);

            return Update(model, true);
        }

        public GenConfig GetGenConfig(string name)
        {
            //开始拼装查询条件
            var predicate = Expressionable.Create<GenConfig>();

            //搜索条件查询语法参考Sqlsugar
            predicate = predicate.AndIF(!string.IsNullOrEmpty(name), it => it.ConfigName == name);
            predicate = predicate.AndIF(!int.IsNegative(0), it => it.Enabled == 0);

            var response = Queryable().Where(predicate.ToExpression()).First();

            return response;

        }


    }
}